package com.cash.lemoloan.statisticlib.Utils;


import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.Telephony;
import android.util.Log;

import com.cash.lemoloan.statisticlib.StatisticApplication;
import com.cash.lemoloan.statisticlib.model.StatisticSmsInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//获取短信记录
public class SmsUtil {

    private static SmsUtil smsUtil;

    public static SmsUtil getInstance() {
        if (smsUtil == null) {
            smsUtil = new SmsUtil();
        }
        return smsUtil;
    }

    //    获取近三十天的短信数据
    public synchronized static List<StatisticSmsInfo> getSmsInfo() {
        long lastYearDate = getLastYearDate();
        List<StatisticSmsInfo> smsLogsInfo = new ArrayList<>();
        Cursor cursor = null;
        try {
            cursor = StatisticApplication.getInstance()
                    .getContentResolver().query(Telephony.Sms.CONTENT_URI,
                            new String[]{Telephony.Sms._ID
                                    , Telephony.Sms.ADDRESS
                                    , Telephony.Sms.PERSON
                                    , Telephony.Sms.BODY
                                    , Telephony.Sms.DATE
                                    , Telephony.Sms.READ
                                    , Telephony.Sms.SEEN
                                    , Telephony.Sms.STATUS
                                    , Telephony.Sms.SUBSCRIPTION_ID
                                    , Telephony.Sms.TYPE} // 1接收 ; 2发送; 3草稿; 4发件箱; 5发送失败; 6待发送列表;
                            , null, null, Telephony.Sms.DEFAULT_SORT_ORDER
                    );
            if (cursor == null) {
                return null;
            }
            while (cursor.moveToNext()) {
                @SuppressLint("Range") String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
                @SuppressLint("Range") String person = cursor.getString(cursor.getColumnIndex(Telephony.Sms.PERSON));
                @SuppressLint("Range") String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
                @SuppressLint("Range") long date = cursor.getLong(cursor.getColumnIndex(Telephony.Sms.DATE));
                @SuppressLint("Range") int type = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.TYPE));
//                该字段指示短信是否已被阅读。其值通常为 0 或 1;0 表示未读; 1 表示已读
                @SuppressLint("Range") int read = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.READ));
//                该字段指示短信是否被查看 0 表示未查看; 1 表示已查看
                @SuppressLint("Range") int seen = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.SEEN));
//                该字段表示短信的发送状态 0：成功。-1：失败（发送失败）. 1：正在发送。2：等待发送。3：已发送。
                @SuppressLint("Range") int status = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.STATUS));
//                该字段指示短信来源的 SIM 卡的 ID（在多 SIM 卡设备上）。它的值是一个整数，表示特定的 SIM 卡。1：表示第一个 SIM 卡。2：表示第二个 SIM 卡。
                @SuppressLint("Range") int subscriptionId = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.SUBSCRIPTION_ID));
                if (date >= lastYearDate) {
                    StatisticSmsInfo smsLogInfo = new StatisticSmsInfo();
                    smsLogInfo.setStatisticaddress(address);
                    smsLogInfo.setStatisticperson(person);
                    smsLogInfo.setStatisticbody(body);
                    smsLogInfo.setStatisticdate(String.valueOf(date));
                    smsLogInfo.setStatistictype(String.valueOf(type));
                    smsLogInfo.setStatisticread(String.valueOf(read));
                    smsLogInfo.setStatisticseen(String.valueOf(seen));
                    smsLogInfo.setStatisticstatus(String.valueOf(status));
                    smsLogInfo.setStatisticsubscription_id(String.valueOf(subscriptionId));
                    smsLogsInfo.add(smsLogInfo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                try {
                    cursor.close();
                } catch (Exception e) {
                }
            }
            return smsLogsInfo;
        }
    }

    private static long getLastMonthDate() {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.MONTH, -1);
        Date lastMonth = ca.getTime(); //结果
        long lastMonthDate = lastMonth.getTime();
        return lastMonthDate;
    }

    private static long getLastYearDate() {
        Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
        ca.setTime(new Date()); //设置时间为当前时间
        ca.add(Calendar.YEAR, -1);
        Date lastYear = ca.getTime(); //结果
        long lastYearDate = lastYear.getTime();
        return lastYearDate;
    }

    public int getSmsCount() {
        ContentResolver resolver = StatisticApplication.getInstance().getContentResolver();
        if (resolver == null) {
            return -1;
        }
        Cursor cursor = StatisticApplication.getInstance()
                .getContentResolver().query(Telephony.Sms.CONTENT_URI,
                        new String[]{Telephony.Sms._ID
                                , Telephony.Sms.ADDRESS
                                , Telephony.Sms.PERSON
                                , Telephony.Sms.BODY
                                , Telephony.Sms.DATE
                                , Telephony.Sms.TYPE}
                        , null, null, Telephony.Sms.DEFAULT_SORT_ORDER
                );
        int count = (cursor == null ? -1 : cursor.getCount());
        if (cursor != null) {
            cursor.close();
        }
        return count;
    }
}
